package com.kedou.sdk.ui.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.kedou.sdk.params.RedpackageResponseParams;
import com.kedou.sdk.utils.ClickUtil;
import com.kedou.sdk.utils.ResUtils;

public class RedPackageTixianFragment extends Fragment  implements View.OnClickListener {

//    private EditText mIdentificationNameEt;
//    private EditText mIdentificationCardEt;
//
//    private Button mBindPhoneBtn;


//    public static RedPackageFragment newInstance(Bundle bundle) {
//        RedPackageFragment fragment = new RedPackageFragment();
//        fragment.setArguments(bundle);
//        return fragment;
//    }

    private ChangeFragmentLister changeFragmentLister;
    private ImageView backImage;
    private LinearLayout bindLayout;
    private TextView moneyText,alipayText,shuomingTxt;
    private RedpackageResponseParams.RedpackageResponseData data ;
    private Button confirmBtn;
    private GridView gridView;
    private BaseAdapter mAdapter;
    private int seletPosition = 0;

    public RedPackageTixianFragment(ChangeFragmentLister changeFragment){
        this.changeFragmentLister = changeFragment;
    }

    public void updateData(RedpackageResponseParams.RedpackageResponseData data ){
        this.data = data;
    }



    @Override
    public void onResume() {
        super.onResume();
        setData();
    }

    String[] mMoney;
    private void setData(){
        if(data==null)return;
        moneyText.setText(data.getBalance());
        if(data.getIs_bind_zfb()==0){
            alipayText.setText("绑定支付宝");
        }else {
            alipayText.setText("已绑定");
        }
        shuomingTxt.setText(data.getWithdraw_remark());
        mMoney = data.getWithdraw_amount();
//        String moneys = data.getWithdraw_amount();
//        if(moneys.startsWith("[")){
//            moneys = moneys.substring(1,moneys.length()-1);
//            Log.e("51wan",moneys);
//            if(moneys.contains(",")){
//                mMoney =moneys.split(",");
//            }else {
//                mMoney= new String[]{moneys};
//            }
        }


//    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "hnssn_fragment_redpackage_tixian");
        View contentView = inflater.inflate(resource, null);
        setView(contentView);

        return contentView;
    }

    private void setView(final View view){
        view.setOnClickListener(this);
        backImage = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "fragment_redpackagetixian_back"));
        backImage.setOnClickListener(this);
        bindLayout = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "fragment_redpackagetixian_alipay_layout"));
        bindLayout.setOnClickListener(this);
        moneyText = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "fragment_redpackagetixian_money"));
        alipayText = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "fragment_redpackagetixian_alipay"));
        shuomingTxt = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "fragment_redpackagetixian_shuoming"));
        gridView = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "fragment_redpackagetixian_gridview"));
        confirmBtn = view.findViewById(ResUtils.getViewIdentifier(getActivity(), "fragment_redpackagetixian_button"));
        confirmBtn.setOnClickListener(this);
        mAdapter = new BaseAdapter() {
            @Override
            public int getCount() {
                return mMoney==null?0:mMoney.length;
            }

            @Override
            public Object getItem(int position) {
                return mMoney[position];
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder;
                if (convertView == null) {
                    viewHolder =new ViewHolder();
                    final Context context = parent.getContext();
                    LayoutInflater inflater = LayoutInflater.from(context);
                    final int layoutResId= ResUtils.getLayoutIdentifier(context, "hnssn_item_redpackage_money");
                    final int titleResId = ResUtils.getViewIdentifier(context, "redpackage_gridview_money");
                    convertView = inflater.inflate(layoutResId, parent, false);
                    viewHolder.title = convertView.findViewById(titleResId);
                    convertView.setTag(viewHolder);
                }else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                viewHolder.title.setText(mMoney[position]);
                if(position == seletPosition){
                    viewHolder.title.setBackgroundResource(ResUtils.getDrawableIdentifier(getContext(),"hn669_redpackage_gridview_banckground_select"));
                    viewHolder.title.setTextColor(Color.parseColor("#e03131"));
                }else {
                    viewHolder.title.setBackgroundResource(ResUtils.getDrawableIdentifier(getContext(),"hn669_redpackage_gridview_banckground"));
                    viewHolder.title.setTextColor(Color.parseColor("#333333"));
                }
                return convertView;
            }
        };
        gridView.setAdapter(mAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                seletPosition=position;
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    public static class ViewHolder {
        public TextView title;
    }

    @Override
    public void onClick(View v) {
        if( v == backImage){
            changeFragmentLister.changeToMain();
        }else if (v == bindLayout){
            changeFragmentLister.changeToBind();
        }else if(v == confirmBtn){
            if(ClickUtil.isFastClick()){
                changeFragmentLister.tixian(mMoney[seletPosition]);
            }
        }
    }
}
